package com.yuanchu.order.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.order.dto.CommentImageDto;
import com.yuanchu.order.dto.EditOrderCommentDto;
import com.yuanchu.order.dto.OrderCommentParamsDto;
import com.yuanchu.order.dto.QueryCommentParamsDto;
import com.yuanchu.order.entity.YoshopOrderGoodsEntity;
import com.yuanchu.order.service.YoshopCommentService;
import com.yuanchu.order.utils.SecurityUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-25 20:24
 */
@RestController
public class CommentController {

    @Resource
    private YoshopCommentService commentService;
    int storeId = 10001;

    @ApiOperation(value = "/order/comment/list", tags = "查询评价订单")
    @GetMapping("/comment/list")
    public R queryOrderComment(@RequestParam("orderId") Integer orderId){
        Integer userId = SecurityUtil.getUserId();
        List<YoshopOrderGoodsEntity> goodsList = commentService.queryOrderComment(orderId, userId);
        Map<String, Object> map = new HashMap<>();
        map.put("goodsList", goodsList);
        return R.ok().put("data", map);
    }

    @ApiOperation(value = "/order/goods/comment/list", tags = "查询评价订单")
    @GetMapping("/goods/comment/list")
    public R queryGoodsComment(QueryCommentParamsDto params){
        PageResult<CommentImageDto> comment = commentService.queryGoodsComment(params, storeId);
        return R.ok().putPage("data", comment);
    }

    @ApiOperation(value = "/order/comment/submit", tags = "提交订单评价")
    @PostMapping("/comment/submit")
    public R submitOrderComment(@RequestBody() OrderCommentParamsDto params){
        Integer userId = SecurityUtil.getUserId();
        boolean submitted = commentService.submitOrderComment(params, userId);
        if (!submitted){
            return R.error("订单已评价");
        }
        return R.ok("提交订单评价成功");
    }

    @PostMapping("/goods/comment/edit")
    public R editOrderComment(@RequestBody() EditOrderCommentDto params){
        commentService.editOrderComment(params, storeId);
        return R.ok();
    }
    @PostMapping("/goods/comment/delete")
    public R deleteOrderComment(@RequestBody() EditOrderCommentDto params){
        commentService.deleteOrderComment(params, storeId);
        return R.ok();
    }

    @GetMapping("/goods/comment/detail")
    public R queryGoodsCommentDetail(@RequestParam("commentId") Integer commentId){
        CommentImageDto comment = commentService.queryGoodsCommentDetail(commentId, storeId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", comment);
        return R.ok().put("data", map);
    }


}
